#!/bin/bash

# TODO: check for GNU mktemp and sed (from coreutils), else exit
# ---------------------------------------------------------------------
# solution below is based on
# <http://www.ooblick.com/weblog/2011/05/12/a-couple-of-shell-quickies/>

# Wrapper function for GNU mktemp
gnu_mktemp() {
	mktemp "$@"
}

# Wrapper function for BSD mktemp
bsd_mktemp() {
	mktemp -t tmpfile.XXXXXX "$@"
}

# Try to figure out which wrapper to use
if mktemp -V | grep version >/dev/null 2>&1; then
	MKTEMP=gnu_mktemp
else
	MKTEMP=bsd_mktemp
fi

#mytmpfile=`$MKTEMP`
echo "MKTEMP to be used: $MKTEMP"

# ---------------------------------------------------------------------

echo -e "\n##### Extension Tests #####"

cd "$(dirname "$0")"

has_py_coverage=false
py_cover_files=$( $MKTEMP )
failed_tests=$( $MKTEMP )

if coverage.py erase >/dev/null 2>/dev/null; then
  has_py_coverage=true
  cover_py_cmd=coverage.py
  else
    if coverage erase >/dev/null 2>/dev/null; then
      has_py_coverage=true
      cover_py_cmd=coverage
      else
        if python-coverage erase >/dev/null 2>/dev/null; then
          has_py_coverage=true
          cover_py_cmd=python-coverage
          else
            if coverage-script.py erase >/dev/null 2>/dev/null; then
            has_py_coverage=true
            cover_py_cmd=coverage-script.py
            fi
        fi
    fi
fi

if $has_py_coverage; then
    echo -e "\nRunning tests with coverage (${cover_py_cmd})"
fi
#if $has_py_coverage; then
#  $cover_py_cmd -e
#fi

function run_py_test() {
  echo -e "\n>>>>>> Testing $1 <<<<<<\n"
  if $has_py_coverage; then
    if ! $cover_py_cmd run -a "$1.test.py"; then
      echo "$1" >> $failed_tests
    fi
    echo "../$1.py" >> $py_cover_files
  else
    if ! python "$1.test.py"; then
      echo "$1" >> $failed_tests
    fi
  fi
  return 0
}

tot_FAILED=0

# TODO: check for GNU mktemp and sed (from coreutils), else exit
# ---------------------------------------------------------------------
# solution below is based on
# <http://notmuchmail.org/pipermail/notmuch/2011/004579.html>

if [ `sed --version >/dev/null 2>/dev/null && echo 1` ]; then
    SED_EXTENDED='sed -r' # GNU sed (e.g. on Linux)
else
    SED_EXTENDED='sed -E' # BSD sed (e.g. on Mac OS X)
fi

echo -e "sed regex command: $SED_EXTENDED\n"

# ---------------------------------------------------------------------

for testFile in *.test.py; do
  if ! run_py_test $( echo $testFile | $SED_EXTENDED 's/^([^.]+)..*$/\1/' ); then
    let tot_FAILED++
  fi
done

if $has_py_coverage; then
  echo -e "\n>> Coverage Report:"
  cat $py_cover_files | xargs $cover_py_cmd report
fi

fail=false
if ! test -z "$( cat $failed_tests )"; then
  echo -e "\nFailed $( cat $failed_tests | wc -l ) of $( ls -1 *.test.py | wc -l ) extension tests:"
  cat $failed_tests | sed 's/^/  - /'
  fail=true
fi
echo ""

rm $py_cover_files $failed_tests

if [ x$fail == xtrue ]; then
	exit 1
else
	exit 0
fi
